package com.ymatou.socialbizer.testcase;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.socialbizer.parameter.CommonBean;
import com.ymatou.socialbizer.service.CommonCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P3;
import com.ymttest.database.mongo.NoteMongoWapper;
/**
 * 
 * */
public class Ts_Common {
	private static CommonBean commonBean;
	private static CommonCall commonCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("通用笔记搜索接口");
	}

	@Before
	public void caseUp() {
		commonBean=new CommonBean();
		commonCall=new CommonCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(commonCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Common_001() {
		Logger.start(true,"通用笔记搜索-验证接口返回公共数据 ");
		try {
			String sort = "addtime";
			int sellerid= 20001778;
			int newstatus = 1;
			//设置参数
			commonBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			commonBean.setLimit("0,10");
			commonBean.setPlatform("pc");
			commonBean.setSort("desc("+sort+")");
			commonBean.setFilter("and(term(sellerid,"+sellerid+"),term(newstatus,"+newstatus+"))");
			commonBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,noteversion,addtime,newstatus");
			commonBean.setExplain("false");
			
			commonCall.setData(commonBean);
			commonCall.callService();	
			
			JSONArray hits = commonCall.getHits();
			
			Logger.verifyEquals("OK",commonCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(commonCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, commonCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyNotNull(commonCall.getCommonInfo("requestid"), "检查requestid是否有返回值");
			Logger.verifyEquals(false, commonCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyEquals(sort, commonCall.getCommonInfo("algorithm"), "验证返回排序方式是否为"+sort);
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Common_002() {
		Logger.start(true,"通用笔记搜索-当有多条数据返回时候验证数据是否重复 ");
		try {
			String sort = "addtime";
			int sellerid= 20001778;
			int newstatus = 1;
			//设置参数
			commonBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			commonBean.setLimit("0,10");
			commonBean.setPlatform("pc");
			commonBean.setSort("desc("+sort+")");
			commonBean.setFilter("and(term(sellerid,"+sellerid+"),term(newstatus,"+newstatus+"))");
			commonBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,noteversion,addtime,newstatus");
			commonBean.setExplain("false");
			
			commonCall.setData(commonBean);
			commonCall.callService();	
			
			JSONArray hitsAry = commonCall.getHits();
			
			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String id = json.getString("noteid");
				
				if (list.contains(id) == true) {

					Logger.verifyEquals(false, list.contains(id), "验证第" + i+ "条返回数据的noteid存在重复值" + id);

				}else{
					Logger.verifyEquals(false, list.contains(id), "验证第" + i+ "条返回数据的noteid不存在重复值" + id);
					list.add(id);
				}
			
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Common_003() {
		Logger.start(true,"通用笔记搜索-验证接口返回数据总数 与数据库查询结果是否一致");
		try {
			String sort = "addtime";
			int sellerid= 3383;
			int newstatus = 0;
			//设置参数
			commonBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			commonBean.setLimit("0,10");
			commonBean.setPlatform("pc");
			commonBean.setSort("desc("+sort+")");
			commonBean.setFilter("and(term(sellerid,"+sellerid+"),term(newstatus,"+newstatus+"))");
			commonBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,noteversion,addtime,newstatus");
			commonBean.setExplain("false");
			
			commonCall.setData(commonBean);
			commonCall.callService();	
			
			//连接mongo，查询买手下的所有通过笔记数
			int number = new NoteMongoWapper().getPassCount(sellerid);
			//获取接口返回该买手下的总数
			int total = commonCall.getTotal();
			Logger.verifyEquals(number, total, "比较接口返回买手id:"+sellerid+"通过的笔记数是与数据库查询结果否相等");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Common_004() {
		Logger.start(true,"通用笔记搜索-检查接口返回数据字段信息是否完整");
		try {
			String sort = "addtime";
			int sellerid= 3383;
			int newstatus = 0;
			//设置参数
			commonBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			commonBean.setLimit("0,10");
			commonBean.setPlatform("pc");
			commonBean.setSort("desc("+sort+")");
			commonBean.setFilter("and(term(sellerid,"+sellerid+"),term(newstatus,"+newstatus+"))");
			commonBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,noteversion,addtime,newstatus");
			commonBean.setExplain("false");
			
			commonCall.setData(commonBean);
			commonCall.callService();	
			
			Logger.verifyEquals("OK", commonCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = commonCall.getHits();
			
			for(int i=1;i<=hitsAry.length();i++){
				
			 String info = hitsAry.get(i-1).toString();
			 JSONObject infoAry = new JSONObject(info);
			
			//获取接口返回的key校验返回字段
			  Logger.verifyEquals(true,infoAry.has("content") , "验证第"+i+"条记录返回结果中有content这个key");
			  Logger.verifyEquals(true, infoAry.has("commentnum"), "验证第"+i+"条记录返回结果中有commentnum这个key");
			  Logger.verifyEquals(true,infoAry.has("collectionnum") , "验证第"+i+"条记录返回结果中有collectionnum这个key");
			  Logger.verifyEquals(true,infoAry.has("noteid") , "验证第"+i+"条记录返回结果中有noteid这个key");
			  Logger.verifyEquals(true,infoAry.has("pics") , "验证第"+i+"条记录返回结果中有pics这个key");
			  Logger.verifyEquals(true,infoAry.has("userid") , "验证第"+i+"条记录返回结果中有userid这个key");
			  
				if (infoAry.has("content") && infoAry.has("noteid") && infoAry.has("pics") && infoAry.has("userid")) {
					
					Logger.verifyNotNull(infoAry.getString("content"), "验证第" + i + "条记录返回结果中有content不为空且不为0");
					Logger.verifyNotNull(infoAry.getString("noteid"), "验证第" + i+ "条记录返回结果中有noteid商品图不为空且不为0");
					Logger.verifyNotNull(infoAry.getString("pics"), "验证第" + i+ "条记录返回结果中有pics商品数不为空且不为0");
					Logger.verifyNotNull(infoAry.getString("userid"), "验证第" + i+ "条记录返回结果中有userid热词不为空且不为0");
				
				}
			
			}
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Common_005() {
		Logger.start(false,"通用笔记搜索-待审核数据不会被搜索出来");
		try {
			String sort = "addtime";
			int sellerid= 20001501;
			int newstatus = 0;
			int noteid = 1001417819;//该买手名下未通过审核的笔记id
			//设置参数
			commonBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			commonBean.setLimit("0,10");
			commonBean.setPlatform("pc");
			commonBean.setSort("desc("+sort+")");
			commonBean.setFilter("and(term(sellerid,"+sellerid+"),term(newstatus,"+newstatus+"))");
			commonBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,noteversion,addtime,newstatus");
			commonBean.setExplain("false");
			
			commonCall.setData(commonBean);
			commonCall.callService();	
			
			//连接mongo查询出一批未通过审核的数据
		//	List<Map> noteList =  new NoteMongoWapper().selectPart10(sellerid,1);
			//获取接口返回的所有笔记数据
			JSONArray hitsAry = commonCall.getHits();

			for(int i=1;i<=hitsAry.length();i++){
				
			 String info = hitsAry.get(i-1).toString();
			 JSONObject infoAry = new JSONObject(info);
			 int noteId = infoAry.getInt("noteid");
			 
			 Logger.verifyEquals(true, noteid !=noteId, "检查该买手名下待审核的笔记接口是否会返回");
			
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Common_006() {
		Logger.start(false,"通用笔记搜索-未通过审核数据不会被搜索出来");
		try {
			String sort = "addtime";
			int sellerid= 20001778;
			int newstatus = 0;
			int noteid = 1001416785;
			//设置参数
			commonBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			commonBean.setLimit("0,10");
			commonBean.setPlatform("pc");
			commonBean.setSort("desc("+sort+")");
			commonBean.setFilter("and(term(sellerid,"+sellerid+"),term(newstatus,"+newstatus+"))");
			commonBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,noteversion,addtime,newstatus");
			commonBean.setExplain("false");
			
			commonCall.setData(commonBean);
			commonCall.callService();	
			JSONArray hitsAry = commonCall.getHits();

			for(int i=1;i<=hitsAry.length();i++){
				
			 String info = hitsAry.get(i-1).toString();
			 JSONObject infoAry = new JSONObject(info);
			 int noteId = infoAry.getInt("noteid");
			 
			 Logger.verifyEquals(true, noteid !=noteId, "检查该买手名下未通过审核的笔记接口是否会返回");
			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
